home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / DocViewToolbar < prev    next >
Text File  |  2003-02-24  |  9KB  |  263 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "DocViewToolbar.h"
  39. #include "DocView.h"
  40. #include "guilib:gfx.h"
  41. #include "UserEvents.h"
  42. #include "Document.h"
  43. #include "outline.h"
  44.  
  45. DocViewToolbar::DocViewToolbar(DocView& docView,GuiWindow& ancestor)
  46.  : GuiWindow("toolbar"),
  47.    view(docView),
  48.    page(*this,0xe),
  49.    lastPage(*this,8),
  50.    pageSlider(*this,0xf),
  51.    outline(*this,0x89),
  52.    logicalPageOffset(0),
  53.    currentPage(0),
  54.    pageCount(1),
  55.    oldWidth(-1),
  56.    history(docView,*this,ancestor),
  57.    bookmarks(docView,*this,ancestor),
  58.    showOutlineTarget(&ancestor,User_ShowOutline,this,DocViewToolbar::showOutline),
  59.    adjusterClickedTarget(&ancestor,User_AdjusterClicked,this,DocViewToolbar::adjusterClicked),
  60.    readPageTarget(&ancestor,User_ReadPage,this,DocViewToolbar::readPage),
  61.    sliderValueChangedTarget(&ancestor,GuiSlider::ValueChanged::Event,
  62.                             this,DocViewToolbar::sliderValueChanged),
  63.    nextPartPageTarget(&ancestor,User_NextPartPage,this,DocViewToolbar::nextPage),
  64.    nextPageTarget(&ancestor,User_NextPage,this,DocViewToolbar::nextPage),
  65.    fastNextPageTarget(&ancestor,User_FastNextPage,this,DocViewToolbar::nextPage),
  66.    previousPartPageTarget(&ancestor,User_PreviousPartPage,this,DocViewToolbar::previousPage),
  67.    previousPageTarget(&ancestor,User_PreviousPage,this,DocViewToolbar::previousPage),
  68.    fastPreviousPageTarget(&ancestor,User_FastPreviousPage,this,DocViewToolbar::previousPage),
  69.    logicalPageNumberATBSTarget(&ancestor,User_LogicalPageNumberATBS,
  70.                                this,DocViewToolbar::logicalPageNumberATBS),
  71.    logicalPageNumberSetTarget(&ancestor,User_LogicalPageNumberSet,
  72.                               this,DocViewToolbar::logicalPageNumberSet)
  73.  
  74. {
  75. }
  76.  
  77. //*************************************************************************
  78.  
  79. DocViewToolbar::~DocViewToolbar() 
  80. {
  81.   Outline* o=view.getDocument().getOutline();
  82.   if (o) o->close(view);
  83. }
  84.  
  85. //*************************************************************************
  86.  
  87. void DocViewToolbar::goBack()
  88. {
  89.   const DocPage* page=history.back();
  90.   if (page) setPage(page->page,0);
  91. }
  92.  
  93. //*************************************************************************
  94.  
  95. void DocViewToolbar::openWindow(GuiWindow& parent,GuiGetWindowStateBlock& parent_state)
  96. {
  97.   GuiGetWindowStateBlock b;
  98.   getState(b);
  99.   b.behind=parent_state.behind;
  100.   parent_state.behind=b.windowHandle;
  101.  
  102.   b.visibleArea.xmin=parent_state.visibleArea.xmin;
  103.   b.visibleArea.xmax=b.visibleArea.xmin+parent_state.visibleArea.getWidth();
  104.   int ht=b.visibleArea.getHeight();
  105.   b.visibleArea.ymax=parent_state.visibleArea.ymax;
  106.   b.visibleArea.ymin=b.visibleArea.ymax-ht;
  107.   show(b,&parent);
  108.  
  109.   int new_width=parent_state.visibleArea.getWidth();
  110.   if (new_width!=oldWidth)
  111.   {
  112.     oldWidth=new_width;
  113.     GuiBBox box;
  114.     pageSlider.getBBox(box);
  115.     box.xmax=new_width-6;
  116.     pageSlider.move(box);
  117.   }
  118.   return;
  119. }
  120. //*************************************************************************
  121.  
  122. void DocViewToolbar::setFirstPage(int page_ ,int logicalPO, int page_count,bool add_to_history)
  123. {
  124.   if (page_count<=0) page_count=1;
  125.   if (page_ >= page_count) page_=page_count;   
  126.   if (page_ <=0) page_=1;
  127.  
  128.   page.setFocus();
  129.   logicalPageOffset=logicalPO;
  130.  
  131.   currentPage=page_;
  132.   pageCount=page_count;
  133.  
  134.   page.setNumber(currentPage+logicalPageOffset);
  135.   lastPage.setNumber(pageCount);
  136.  
  137.   // crashes if upper==lower
  138.   pageSlider.setBounds( GuiSlider::SetUpper | GuiSlider::SetLower | GuiSlider::SetStepSize,
  139.                         1,(pageCount==1?2:pageCount),1);
  140.   pageSlider.setValue(currentPage);
  141.  
  142.   Outline* o=view.getDocument().getOutline();
  143.   outline.fade(!(o && o->isOk()));
  144.  
  145.   if (add_to_history) history.add(currentPage);
  146. }
  147.  
  148. //*************************************************************************
  149.  
  150. void DocViewToolbar::setPage(int new_page, bool set_history)
  151. {
  152.   if (new_page>pageCount) new_page=pageCount;
  153.   if (new_page<=0) new_page=1;
  154.   if (new_page==currentPage) return;
  155.   currentPage=new_page;
  156.   if (set_history) history.add(currentPage);
  157.   pageSlider.setValue(currentPage);
  158.   page.setNumber(currentPage+logicalPageOffset);
  159.  
  160.   view.showPage();
  161. }
  162.  
  163. //*************************************************************************
  164.  
  165. Claim DocViewToolbar::showOutline(GuiToolboxEvent&,const GuiIdBlock&)
  166. {
  167.   Outline* o=view.getDocument().getOutline();
  168.   if (o) o->open(view);
  169.   outline.fade(!(o && o->isOk()));
  170.   return CLAIM;
  171. }
  172. //*************************************************************************
  173.  
  174. Claim DocViewToolbar::adjusterClicked(GuiToolboxEvent& ev,const GuiIdBlock& id_block)
  175. {  
  176.   if (id_block.self.component==0x87)
  177.   {
  178.     ev.eventCode=User_PreviousPartPage;
  179.     if (gfx::inkey(-1)) ev.eventCode=User_PreviousPage; //shift
  180.     if (gfx::inkey(-2)) ev.eventCode=User_FastPreviousPage; //ctrl
  181.     return previousPage(ev,id_block);
  182.   }
  183.  
  184.   ev.eventCode=User_NextPartPage;
  185.   if (gfx::inkey(-1)) ev.eventCode=User_NextPage; //shift
  186.   if (gfx::inkey(-2)) ev.eventCode=User_FastNextPage; //ctrl
  187.   return nextPage(ev,id_block);
  188. };
  189.  
  190. //*************************************************************************
  191.  
  192. Claim DocViewToolbar::readPage(GuiToolboxEvent&,const GuiIdBlock&)
  193.   setPage(page.getNumber()-logicalPageOffset);
  194.   view.scroll(DocView::SCROLL_TOP);
  195.   return CLAIM;
  196. };
  197.  
  198. //*************************************************************************
  199.  
  200. Claim DocViewToolbar::sliderValueChanged(GuiToolboxEvent& ev,const GuiIdBlock&)
  201. {
  202.   GuiSlider::ValueChanged& s= (GuiSlider::ValueChanged&)ev;
  203.  
  204.   if (s.flags == GuiSlider::ValueChanged::DragStart) history.addPending();
  205.   if (s.newValue<=pageCount && currentPage!=s.newValue)
  206.   {
  207.     currentPage=s.newValue;
  208.     page.setNumber(currentPage+logicalPageOffset);
  209.     view.showPage();
  210.     history.addPending(currentPage);
  211.   }
  212.   return CLAIM;
  213. }
  214.  
  215. //*************************************************************************
  216.  
  217. Claim DocViewToolbar::nextPage(GuiToolboxEvent& ev,const GuiIdBlock&)
  218. {
  219.  
  220.   if (ev.eventCode==User_NextPartPage && view.scroll(DocView::SCROLL_DOWN)) return CLAIM;
  221.  
  222.   int page=currentPage;
  223.   setPage(currentPage+(ev.eventCode==User_FastNextPage?10:1));
  224.   if (page!=currentPage) view.scroll(DocView::SCROLL_TOP);
  225.  
  226.   return CLAIM;
  227. }
  228.  
  229. //*************************************************************************
  230.  
  231. Claim DocViewToolbar::previousPage(GuiToolboxEvent& ev,const GuiIdBlock&)
  232. {
  233.   if (ev.eventCode==User_PreviousPartPage && view.scroll(DocView::SCROLL_UP)) return CLAIM;
  234.  
  235.   int page=currentPage;
  236.   setPage(currentPage-(ev.eventCode==User_FastPreviousPage?10:1));
  237.   if (page!=currentPage) 
  238.      view.scroll(ev.eventCode==User_PreviousPartPage ? DocView::SCROLL_BOTTOM : DocView::SCROLL_TOP);
  239.   return CLAIM;
  240. }
  241.  
  242. //*************************************************************************
  243.  
  244. Claim DocViewToolbar::logicalPageNumberATBS(GuiToolboxEvent&,const GuiIdBlock& idBlock)
  245. {
  246.   GuiWindow win(idBlock.self.id);
  247.   NumberField(win,0).setNumber(currentPage+logicalPageOffset);
  248.   return CLAIM;
  249. }
  250.  
  251. //*************************************************************************
  252.  
  253. Claim DocViewToolbar::logicalPageNumberSet(GuiToolboxEvent&,const GuiIdBlock& idBlock)
  254. {
  255.   
  256.   GuiWindow win(idBlock.self.id);
  257.   logicalPageOffset = NumberField(win,0).getNumber()-currentPage;
  258.   page.setNumber(currentPage+logicalPageOffset);
  259.  
  260.   return CLAIM;
  261. }
  262.